﻿/**
 *	html/monitor/center/scene/panel_protocal.js
 *
 *	(C) 2014 Yuanjj
 *
 * 	history:
 * 		2014-07-15		create the file
 */

/**
 *	create the protocal panel
 *
 *	@panel_center		[in] the center panel object
 *	@showflag			[in] the show flag ( "show" or "hide" )
 *
 *	return
 *		void
 */
function create_protocal_panel( panel_center, showflag )
{
    var panel = new Ext.panel.Panel
    ({
         id:"panel_c_protocal",
         layout:"vbox",
         flex:1,
         width:"100%",
         listeners:{ afterrender:_create_protocal_component }
    });

    if ( "show" === showflag )
        panel.show();
    else
        panel.hide();

    panel_center.add_subpanel( panel );
}

/**
 *	create the components for protocal
 *
 *	@panel_c_protocal		[in] the protocal panel object
 */
function _create_protocal_component( panel_c_protocal )
{
    var protocal_top_bar	= _create_protocal_top_bar();
    var protocal_grid		= _create_protocal_grid();

    panel_c_protocal.add( protocal_top_bar );
    panel_c_protocal.add( protocal_grid );
}

/**
 *	create the toolbar for protocal panel
 *
 *	return
 *		the toolbar object
 */
function _create_protocal_top_bar()
{
    var protocal_top_bar = new Ext.toolbar.Toolbar
    ({
         id:"protocal_top_bar",
         height:30,
         width:"100%"
    });

    var top_bar_textarea 		= create_form_field_textarea( protocal_top_bar, "规约名称", 50, 150 );
    var top_bar_combox 			= create_form_field_combobox( protocal_top_bar, "通道类型", 50, 150, g_tunnel_type, "type", "local", 60, "type", 0 )
    var top_bar_search_button 	= create_form_field_button( protocal_top_bar, "查询", on_search_protocal );

    protocal_top_bar.add(
        { xtype:"tbspacer", width:10 },
        top_bar_textarea,
        { xtype:"tbspacer", width:10 },
        top_bar_combox,
        { xtype:"tbspacer", width:10 },
        top_bar_search_button,
        "->",
        { xtype:"button", text:"添加", handler:on_add_new_protocal },
        "-",
        { xtype:"button", text:"修改" },
        "-",
        { xtype:"button", text:"删除" }
    );

    protocal_top_bar.search_textarea	= top_bar_textarea;
    protocal_top_bar.search_combobox	= top_bar_combox;
    protocal_top_bar.search_button		= top_bar_search_button;

    return protocal_top_bar;
}

/**
 *	create the grid for protocal panel
 *
 *	return
 *		the grid object
 */
function _create_protocal_grid()
{
    //var protocal_grid	= new Ext.grid.Panel
    var protocal_grid	= Ext.create
    ("Ext.grid.Panel", {
         id:"protocal_grid", width:"100%", flex:1, plugins:g_edit_plugin, store:g_protocal,
         columns:[
             {header:"序号", width:50, dataIndex:"serial" },
             {header:"规约名称", width:150, dataIndex:"pto_name" },
             {header:"通道类型", width:100, dataIndex:"tunnel_type" },
             {header:"实现文件", width:100, dataIndex:"pto_file" },
             {header:"支持的设备", width:200, dataIndex:"dev_probe",
              field:{ xtype:"combobox", maxHeight:60, triggerAction:"all", selectOnTab:true, store:g_device_type,
                      displayField:"probe", queryMode:"local", typeAhead:true, forceSelection:true, valueField:"probe",
                 }
             },
             {header:"备注说明", width:200, dataIndex:"remark" },
         ]
    });

    return protocal_grid;
}

/**
 *	search tunnel type button handler
 */
function on_search_protocal()
{
    var tunneltype 		= this.parent.search_combobox.getRawValue();
    var protocalname	= this.parent.search_textarea.getValue();
    g_protocal.filterBy( function( record )
    {
        if ( "全部" === tunneltype || "" === tunneltype )
        {
            if ( "" == protocalname )
                return true;
            else
                return (record.get( "pto_name" ) === protocalname);
        }
        else
        {
            if ( "" == protocalname )
                return (record.get( "tunnel_type" ) === tunneltype);
            else
                return (record.get( "tunnel_type" ) === tunneltype) && (record.get( "pto_name" ) === protocalname);
        }
    });
}

/**
 *	add new protocal
 */
var g_form_protocal;
function on_add_new_protocal()
{
    if( undefined === g_form_protocal )
    {
        g_form_protocal = _create_protocal_form();
    }

    g_form_protocal.show();
}

/**
 *	create protocal form
 */
function _create_protocal_form()
{
    var form_protocal = Ext.create
    ( "Ext.form.Panel",{
         title:"编辑设备规约", frame:true, renderTo:Ext.getBody(),
         draggable:true, constrain:true, simpleDrag:true, floating:true,
         width:310, height:145,
         listeners:{
             beforeclose:function(){ this.hide(); return false; }
         },
         items:[
             {xtype:"textfield", fieldLabel:"规约名称", labelWidth:60, width:300},
             {xtype:"combobox",	fieldLabel:"通道类型", labelWidth:60, width:300, queryMode:"local", maxHeight:60,
                 store:g_tunnel_type, name:"type", valueField:"type", displayField:"type"
             },
             {xtype:"filefield", fieldLabel:"实现文件", buttonText:"上传文件", labelWidth:60, width:300},
         ],
         buttons:[
             {text:"确定", handler:function(){form_protocal.hide();}},
             {text:"取消", handler:function(){form_protocal.hide();}}
         ]
    });

    return form_protocal;
}
